//
//  MoreViewController.m
//  Movie
//
//  Created by cjy on 16/3/30.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "MoreViewController.h"
#import "NetworkHandeler.h"
#import "OtherCollectionViewCell.h"

//#import "OtherModel.h"

#import "PushMoreViewController.h"
#import "NetHandleBlock.h"
#import "SearchTableViewCell.h"
//#import "ModelForFirst.h"
#import "VCForPushAVPlayer.h"

#import "MovieModel.h"

@interface MoreViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,NetworkHandelerDelegate,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>


@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *arr;
@property (nonatomic,strong)UICollectionView *collectionV;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong)UIView *myView;
@property (nonatomic,assign)BOOL flag;
@property (nonatomic,strong)UISearchBar *searchbar;
@property (nonatomic,strong)NSString *url;
@property (nonatomic,strong)NSMutableArray *arrSearch;


@end

@implementation MoreViewController


- (void)viewWillAppear:(BOOL)animated{

    self.tabBarController.tabBar.hidden = NO;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self handle];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"CT"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    [self createcollection];
    self.myView = [[UIView alloc]init];
    self.myView.frame = CGRectMake(0, 64, self.view.frame.size.width,  self.view.frame.size.height - 64 );
    UIImageView *myviewImaView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"asd"]];
    myviewImaView.frame = CGRectMake(0, 0, self.myView.frame.size.width, self.myView.frame.size.height);
    [self.myView insertSubview:myviewImaView atIndex:1];
//    self.myView.backgroundColor = [UIColor colorWithRed:28 / 255.f green:28 / 255.f blue:28 / 255.f alpha:1];
    [super viewDidLoad];
    
    _flag = NO;

    [self createSearch];

}

- (void)createSearch{
    
    self.searchbar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    self.searchbar.placeholder = @"输入需要搜索的内容";
    self.searchbar.showsCancelButton = YES;
    [self.myView addSubview:self.searchbar];
    
    self.searchbar.delegate = self;
    [self createtableView];

}

- (void)createtableView{

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height - 154) style:UITableViewStylePlain];
    
    
    [self.myView addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"pool"];
//    self.tableView.backgroundColor = [UIColor colorWithRed:28 / 255.f green:28 / 255.f blue:28 / 255.f alpha:1];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    

}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.searchbar resignFirstResponder];
    
    
    
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    if (self.searchbar.text== nil ) {
        self.tableView.backgroundColor =[ UIColor clearColor];
    }else{
        
    self.tableView.backgroundColor = [UIColor colorWithRed:28 / 255.f green:28 / 255.f blue:28 / 255.f alpha:1];        
    }
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{


    self.arrSearch = [NSMutableArray array];
    // 如果有中文将搜索的内容进行转码
    NSString *temp = [self.searchbar.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString *url = @"http://service.vmovier.com/api/search/getList?kw=";
    
    NSString *url2 = @"&p=1";
     self.url = [url stringByAppendingFormat:@"%@%@", temp, url2];

    [NetHandleBlock networkingWithURL:self.url completeHandel:^(NSURLResponse *response, NSData *data, id result) {
       
        
        NSArray *arrdata = [result objectForKey:@"data"];
        for (NSDictionary *dicdata in arrdata) {
          
            MovieModel *model = [[MovieModel alloc]init];
            
            model.coverForDetail = [dicdata objectForKey:@"image"];
            model.title = [dicdata objectForKey:@"title"];
            model.rating = [dicdata objectForKey:@"rating"];
            model.postid = [dicdata objectForKey:@"postid"];
            model.share_num = [dicdata objectForKey:@"share_num"];
            [self.arrSearch addObject:model];
        }
        if (self.arrSearch.count == 0) {
            
                self.tableView.backgroundColor = [UIColor colorWithRed:28 / 255.f green:28 / 255.f blue:28 / 255.f alpha:1];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有搜索到" preferredStyle:UIAlertControllerStyleAlert];
            
            
            UIAlertAction *defaultaction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            }];
            
            
            [alert addAction:defaultaction];
            [self presentViewController:alert animated:YES completion:nil];
            

        };
        
        [self.tableView reloadData];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    
    VCForPushAVPlayer *show = [[VCForPushAVPlayer alloc]init];
    
    MovieModel *model = self.arrSearch[indexPath.row];
    show.idForPush = model.postid;
    show.title = model.title;
    
    
    [self.navigationController pushViewController:show animated:NO];


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arrSearch.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    MovieModel *model = self.arrSearch[indexPath.row];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    cell.backgroundColor = [UIColor colorWithRed:28 / 255.f green:28 / 255.f blue:28 / 255.f alpha:1];
    

    [cell passModel:model];
    return cell;


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;

}

- (void)search{
    

    
    if (_flag == NO) {

        
        self.searchbar.text = nil;
        
        self.myView.alpha = 0;
        //动画
        self.myView.center = CGPointMake(self.view.frame.size.width/2, -self.view.frame.size.height);
        [UIView animateWithDuration:0.7 animations:^{
            self.myView.alpha = 1;
            self.myView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 + 32);
            
        }];
        
               [self.view addSubview:self.myView];
        _flag = 1;
    }
    else if (_flag == YES)
    {
        
        [UIView animateWithDuration:0.7 animations:^{
            self.myView.alpha = 0.5;
            self.myView.center = CGPointMake(self.view.frame.size.width/2,-1900);
        }completion:^(BOOL finished) {
            [self.myView removeFromSuperview];
            _flag = NO;
        }];
        
        
    }
    
    


}

- (void)handle{

    NSString *str = @"http://baobab.wandoujia.com/api/v2/categories?_s=3140041fd648ad284b781c2f0b084edc&f=iphone&net=wifi&u=83e89c9ba853b22d017cf56713c230da417b7509&v=1.12.1&vc=403";
    
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];


}


- (void)handelerDidComplete:(id)result{

    
    self.arr = [NSMutableArray array];
    
    for (NSDictionary *dic  in result) {

        
        MovieModel *model = [[MovieModel alloc]init];
        
        model.coverForDetail = [dic objectForKey:@"bgPicture"];
        model.title = [dic objectForKey:@"name"];
        model.ID = [dic objectForKey:@"id"];
        [self.arr addObject:model];
        
        
    }
    
    [self.collectionV reloadData];
    


}


- (void)createcollection{

    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 30)/2, (self.view.frame.size.width - 30)/2);
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    self.collectionV = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowLayout];
    
    
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.view addSubview:self.collectionV];
    self.collectionV.backgroundColor = [UIColor colorWithRed:28 / 255.f green:28 / 255.f blue:28 / 255.f alpha:1];
    [self.collectionV registerClass:[OtherCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{


    return self.arr.count;

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


    OtherCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    
//    OtherModel *model = self.arr[indexPath.item];
//    cell.alpha = 0.9;
//    [cell passModel:model];

    
    MovieModel *model = self.arr[indexPath.item];
    cell.alpha = 0.9;
    [cell passModel:model];
    return cell;




}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    PushMoreViewController *push = [[PushMoreViewController alloc]init];
    
//    
//    OtherModel *model = self.arr[indexPath.item];
//    push.ida = [model.ID integerValue];

    
    MovieModel *model = self.arr[indexPath.item];
    push.ida = [model.ID integerValue];
    
    [self.navigationController pushViewController:push animated:YES];
        




}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
